package graph;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import object.Utente;

import crawler_twitter.CrawlerUtentiByItem;
import crawler_twitter.Crawlerretweet;

import mysql.DBCache;

public class XML_creator {
	
	public  void createXMLGraphByItem(String item){
		DBCache db = new DBCache();
		int cont=12;// era 10
		int c=0;
		List<Utente>utenti = new LinkedList<Utente>();
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--  An excerpt of an egocentric social network  -->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n<graph edgedefault=\"undirected\">";
		String inizializzazione="<!-- data schema -->\n<key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>\n<key id=\"valutation\" for=\"node\" attr.name=\"valutation\" attr.type=\"string\"/>";
		String nodo1 ="<node id=\"1\">\n<data key=\"name\">"+item+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String nodo2 ="<node id=\"2\">\n<data key=\"name\">"+"BAD"+"</data>\n<data key=\"valutation\">BAD</data>\n</node>";
		String nodo3 ="<node id=\"3\">\n<data key=\"name\">"+"GOOD"+"</data>\n<data key=\"valutation\">GOOD</data>\n</node>";
		String cluster1 ="<node id=\"4\">\n<data key=\"name\">"+"apple"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String cluster2 ="<node id=\"5\">\n<data key=\"name\">"+"windows"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String cluster3 ="<node id=\"6\">\n<data key=\"name\">"+"android"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String cluster1B ="<node id=\"7\">\n<data key=\"name\">"+"apple"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String cluster2B ="<node id=\"8\">\n<data key=\"name\">"+"windows"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String cluster3B ="<node id=\"9\">\n<data key=\"name\">"+"android"+"</data>\n<data key=\"valutation\">item</data>\n</node>";
		String clusterretB ="<node id=\"10\">\n<data key=\"name\">"+"retweet"+"</data>\n<data key=\"valutation\">item</data>\n</node>";// aggiunta
		String clusterretG ="<node id=\"11\">\n<data key=\"name\">"+"retweet"+"</data>\n<data key=\"valutation\">item</data>\n</node>";//aggiunta
		utenti = db.selectSecondCluster(item);
		System.out.println(utenti.size());
		String end ="</graph>\n</graphml>";
	    try {
	      FileOutputStream file = new FileOutputStream("C:\\Users\\Andrea\\Desktop\\graph.xml");
	      PrintStream Output = new PrintStream(file); 
	        Output.println(head);
	        Output.println(inizializzazione);
	        Output.println(nodo1);
	        Output.println(nodo2);
	        Output.println(nodo3);
	        Output.println(cluster1);
	        Output.println(cluster2);
	        Output.println(cluster3);
	        Output.println(cluster1B);
	        Output.println(cluster2B);
	        Output.println(cluster3B);
	        Output.println(clusterretB);//aggiunta
	        Output.println(clusterretG);//aggiunta
	        for(Utente ut:utenti){
	    		System.out.println("utenti "+cont+ut.getNome()+"  "+c+"  "+ut.getSecond_cluster()+" "+ut.getValutation());
	         	if(ut.getValutation().equals("GOOD") && ut.getRetweet().equals("yes")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if(ut.getValutation().equals("BAD") && ut.getRetweet().equals("yes")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if (ut.getValutation().equals("GOOD") && ut.getSecond_cluster().equals("apple")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        		}
	        	else if (ut.getValutation().equals("GOOD") && ut.getSecond_cluster().equals("windows")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if (ut.getValutation().equals("GOOD") && ut.getSecond_cluster().equals("android")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if (ut.getValutation().equals("BAD") && ut.getSecond_cluster().equals("apple")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if (ut.getValutation().equals("BAD") && ut.getSecond_cluster().equals("windows")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	else if (ut.getValutation().equals("BAD") && ut.getSecond_cluster().equals("android")){
	        		Output.print("<node id=\""+cont+"\">\n<data key=\"name\">"+ut.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		ut.setId(cont);
	        		cont++;
	        	}
	        	c++;
	        }
	        Output.println("<edge source=\"1\" target=\""+"2"+"\"></edge>");
	       	Output.println("<edge source=\"1\" target=\""+"3"+"\"></edge>");
	       	Output.println("<edge source=\"3\" target=\""+"4"+"\"></edge>");
	       	Output.println("<edge source=\"3\" target=\""+"5"+"\"></edge>");
	       	Output.println("<edge source=\"3\" target=\""+"6"+"\"></edge>");
	       	Output.println("<edge source=\"3\" target=\""+"11"+"\"></edge>");
	       	Output.println("<edge source=\"2\" target=\""+"7"+"\"></edge>");
	       	Output.println("<edge source=\"2\" target=\""+"8"+"\"></edge>");
	       	Output.println("<edge source=\"2\" target=\""+"9"+"\"></edge>");
	       	Output.println("<edge source=\"2\" target=\""+"10"+"\"></edge>");
	       	for(Utente ut:utenti){
	        	if(ut.getValutation().equals("GOOD") & ut.getRetweet().equals("yes")){
	        		Output.println("<edge source=\""+ut.getId()+"\" target=\""+"11"+"\"></edge>");
	        	}
	        	else if(ut.getValutation().equals("BAD") & ut.getRetweet().equals("yes")){
	        		Output.println("<edge source=\""+ut.getId()+"\" target=\""+"10"+"\"></edge>");
	        	}
	        	else if (ut.getValutation().equals("GOOD") & ut.getSecond_cluster().equals("apple")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"4"+"\"></edge>");
	       			}
	       		else if(ut.getValutation().equals("GOOD") & ut.getSecond_cluster().equals("windows")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"5"+"\"></edge>");
	       		}
	       		else if(ut.getValutation().equals("GOOD") & ut.getSecond_cluster().equals("android")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"6"+"\"></edge>");
	       		}
	       		else if(ut.getValutation().equals("BAD") & ut.getSecond_cluster().equals("apple")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"7"+"\"></edge>");
	       		}
	       		else if(ut.getValutation().equals("BAD") & ut.getSecond_cluster().equals("windows")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"8"+"\"></edge>");
	       		}
	       		else if(ut.getValutation().equals("BAD") & ut.getSecond_cluster().equals("android")){
	       			Output.println("<edge source=\""+ut.getId()+"\" target=\""+"9"+"\"></edge>");
	       		}
	       	}
	        
	        Output.print(end);
	      }

	     catch (IOException e) {
	      System.out.println("Errore: " + e);
	      System.exit(1);
	    }
	
	     System.out.println("File item XML creato Correttamente");
	}
	
	
	public  void createXMLGraphByUser(String utente){
		Crawlerretweet cwt = new Crawlerretweet();
		DBCache db = new DBCache();
		Set<String> utenti = new HashSet<String>();
		List<Utente> utentidb = new LinkedList<Utente>();
		List<Utente> utentifinali = new LinkedList<Utente>();
		utenti = cwt.getListUser(utente);
		utentidb = db.getallAmici();
		System.out.println("Inizio creazione file XML");
		System.out.println(utenti.size());
		int id=4;
		//controllo se nel db c'e l'utente riportato dalla funzione retitter

		for (String s1:utenti){
			Utente u = new Utente();
			u.setId(id);
			u.setNome(s1);
			for (Utente ut:utentidb){
				if (s1.equals(ut.getNome())){
					u.setPresente(true);
				}
				else{
					u.setPresente(false);
				}
			}
			utentifinali.add(u);
			id++;
		}
		/*for (Utente s:utentifinali){
			System.out.println(s.getNome()+" "+s.getId()+s.getPresente());
		}*/
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--  An excerpt of an egocentric social network  -->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n<graph edgedefault=\"undirected\">";
		String inizializzazione="<!-- data schema -->\n<key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>\n<key id=\"valutation\" for=\"node\" attr.name=\"valutation\" attr.type=\"string\"/>";
		String nodo ="<node id=\"1\">\n<data key=\"name\">"+utente+"</data>\n<data key=\"valutation\">user</data>\n</node>";
		String prese ="<node id=\"2\">\n<data key=\"name\">"+"presente"+"</data>\n<data key=\"valutation\">user</data>\n</node>";
		String nonpres ="<node id=\"3\">\n<data key=\"name\">"+"non presente"+"</data>\n<data key=\"valutation\">user</data>\n</node>";
		String end ="</graph>\n</graphml>";
		
		
	    try {
	      FileOutputStream file = new FileOutputStream("C:\\Users\\Andrea\\Desktop\\graph.xml");
	      PrintStream Output = new PrintStream(file); 
	        Output.println(head);
	        Output.println(inizializzazione);
	        Output.println(nodo);
	        Output.println(prese);
	        Output.println(nonpres);

	        for(Utente st:utentifinali){
	        		Output.println("<node id=\""+st.getId()+"\">\n<data key=\"name\">"+st.getNome()+"</data>\n<data key=\"valutation\">");
	        		Output.println(""+"</data>\n</node>");
	        		}
	        
	        Output.println("<edge source=\"1\" target=\""+"2"+"\"></edge>");
	        Output.println("<edge source=\"1\" target=\""+"3"+"\"></edge>");
	   
	        for(Utente uu:utentifinali){
	        	if(uu.getPresente()){
	        		Output.println("<edge source=\"2\" target=\""+uu.getId()+"\"></edge>");
	        			        	}
	        	else{
	        		Output.println("<edge source=\"3\" target=\""+uu.getId()+"\"></edge>");
	        	}
	        }
	        
	        Output.print(end);
	      }

	     catch (IOException e) {
	      System.out.println("Errore: " + e);
	      System.exit(1);
	    }
	
	     System.out.println("File utente XML creato Correttamente");
	}
	public XML_creator() {
		// TODO Auto-generated constructor stub
	}
	public void run(){
		ShowGraph graph = new ShowGraph();
		graph.run();
	}
}